<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form ref="userForm" :model="user" status-icon :rules="rules">
        <div class="title">GK物联网平台</div>
        <el-form-item prop="username">
          <el-input type="text" prefix-icon="el-icon-user" v-model="user.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-view" v-model="user.password" @keyup.enter="login" />
        </el-form-item>
        <el-form-item prop="checkCode" v-show="useCheckImage">
          <el-input type="text" prefix-icon="el-icon-mobile" v-model="user.checkCode" placeholder="验证码" style="width: 60%"
            @keyup.enter="login" />
          <div class="login-code" v-show="useCheckImage">
            <el-image :src="codeUrl" fit="contain" class="check-image" @click="getCheckImage"></el-image>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login">登陆</el-button>
          <div style="float: right;">
            <router-link class="link-type" :to="'/register'">没有账户？去注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="login-footer">
      <div>Copyright © 2022-2023 geniusk.com All Rights Reserved.</div>
      <div class="beian-code">
        <a href="https://beian.miit.gov.cn/" target="_blank">蒙ICP备2021005211号-1</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      user: {
        username: '',
        password: '',
        checkCode: null,
        checkId: ''
      },
      codeUrl: '',
      useCheckImage: false,
      rules: {
        username: [
          {
            required: true,
            message: "请输入账户",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ],
        checkCode: [
          {
            message: "请输入验证码",
            trigger: "blur"
          },
          {
            message: "请输入正确的验证码",
            trigger: "blur",
          },
        ]
      }
    }
  },
  mounted() {
    this.getCheckImage();
  },
  methods: {
    //获取验证码图片
    getCheckImage() {
      this.$api.getCheckImage().then((res) => {
        this.codeUrl = res.checkImg;
        this.user.checkId = res.checkId;
        this.useCheckImage = res.useCheckImage;
      })
    },
    // 登陆
    login() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.$api.login(this.user).then((res) => {
            this.$router.push('/welcome')
          })
        } else {
          return false;
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  width: 100vw;
  height: 100vh;

  .modal {
    width: 400px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0px 0px 10px 3px #c7c9cb4d;

    .title {
      font-size: 25px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }

    .btn-login {
      width: 100%;
    }

    .login-code {
      margin-left: 10px;
      width: 35%;
      height: 30px;
      float: right;

      .check-image {
        width: 100px;
        height: 30px;
        cursor: pointer;
        float: right;
      }
    }
  }

  .login-footer {
    height: 40px;
    line-height: 20px;
    position: fixed;
    bottom: 10vh;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;

    .beian-code {
      a {
        color: #fff;
      }
    }
  }
}
</style>
